<?php
namespace Admin\Controller;
class UserController extends AdminBaseController
{
   public function index()
    {
        if(session('auid')==null){
            $this->display('index/login');
            exit();
        }
        $this->display('user/index');
        
    }
    
    public function ulist(){
        if(session('auid')==null){
            $this->display('index/login');
            exit();
        }
        $mobile=I('mobile','');
        $email=I('email','');
        $uid=I('uid','');
        $cond='';
        if($uid!=''){
            $cond=" and a.id=".$uid;
            $whe['id']=$uid;
        }else{
            if($mobile!='' and $email!=''){
                $cond=" and a.mobile=".$mobile." and a.email='".$email."'";
                $whe['mobile']=$mobile;
                $whe['email']=$email;
            }
            else if($mobile!='' and $email==''){
                $whe['mobile']=$mobile;
                $cond=' and a.mobile='.$mobile;
            } else if($mobile=='' and $email!='') {
                $whe['email']=$email;
                $cond=" and a.email='".$email."'";
            }
        }
        $page=I('page',1);
        $rows=I('rows',50);
        $start=($page-1)*$rows;
        
        $sql="SELECT a.id,a.mobile,a.logindate,a.createdate,a.email,a.signumber,b.avail_money,b.present_money,b.locked_money,b.notpresent_money,b.creditmoney FROM dd_user a LEFT JOIN dd_user_assets b ON a.id=b.uid WHERE 1=1 ".$cond.' ORDER BY  a.createdate LIMIT '.$start.','.$rows;
        $dao = M();
        $list = $dao->query($sql);
        $count = M('User')->where($whe)->count();
        $map=array(
            'rows'=>$list,
            'total'=>$count
        );
        $this->ajaxReturn($map,'JSON');
    }  

    
    public function detail(){
        $id =I('id','');
        if(session('auid')==null){
            $this->display('index/login');
            exit();
        }
        if($id!=''){
            $sql="SELECT a.id,a.username,a.mobile,a.createdate,a.email,a.signumber,b.avail_money,b.present_money,b.locked_money,b.notpresent_money,b.creditmoney FROM dd_user a LEFT JOIN dd_user_assets b ON a.id=b.uid WHERE a.id= ".$id;
            $dao = M();
            $users = $dao->query($sql);
            $this->assign('user',$users[0]);
            
            $credion['loanduid']=$id;
            $credion['status']=array('in','1,3');
            $lixi=M('CredFlow')->where($credion)->sum('interest*(persionnum-rpersionum)');
            $penalty=M('CredFlow')->where($credion)->sum('penalty');
            $credittotal=M('CredFlow')->where($credion)->sum('money');
            $this->assign('lixi',$lixi);
            $this->assign('penalty',$penalty);
            $this->assign('credittotal',$credittotal);
            
            $allDomaincount=M('UserDomains')->where('uid='.$id.' and check_status=3')->count();
            $allmortagecount=M('UserDomains')->where('uid='.$id.' and is_mortage=2 and check_status=3')->count();
            $this->assign('allDomaincount',$allDomaincount);
            $this->assign('allmortagecount',$allmortagecount);
            $checkdomcount=M('UserDomains')->where('uid='.$id.' and check_status=1')->count();
            $this->assign('checkdomcount',$checkdomcount);
        }
        $this->display("user/detail");
    }
    
    
    public function update(){
        $mobile=I('mobile','');
        $email=I('email','');
        $id=I('id','');
        M('User')->where('id='.$id)->setField(array('mobile'=>$mobile,'email'=>$email));
        $this->ajaxReturn(array('status'=>0),'JSON');
    }
    
 public function userpay(){ 
     if(session('auid')==null){
            $this->display('index/login');
            exit();
        }
        $this->display("user/userpay");
    }  

    public function userpaylist(){

      if(session('auid')==null){
            $this->display('index/login');
            exit();
        }
        $page=I('page',1);
        $rows=I('rows',50);
        $start=($page-1)*$rows;
        $mobile=I('mobile','');
        if($mobile!=''){
            $condi['a.mobile']=$mobile;
        }
        $email=I('email','');
        if($email!=''){
            $condi['a.email']=$email;
        }
        $id=I('id','');
        if($id!=''){
            $condi['a.id']=$id;
        }
        $user=M('User');
        $list=$user->join(' as a LEFT JOIN dd_user_assets b ON a.id=b.uid')->field('a.id,a.mobile,a.email,b.avail_money,b.present_money,b.locked_money,b.notpresent_money,b.creditmoney')
        ->where($condi)->order('a.createdate desc')->limit($start,$rows)->select();
        $count = M('User')->count();
        $map=array(
            'rows'=>$list,
            'total'=>$count
        );
        $this->ajaxReturn($map,'JSON');
        exit();
    }
 public function usermoney(){   

      if(session('auid')==null){
            $this->display('index/login');
            exit();
        }
       $id=I('id','');
       if($id==''){
        $map=array('status'=>2);
          $this->ajaxReturn($map,'JSON');
         exit();
       }
       $addmoney=I('addmoney',0);
       $sumemoney=I('sumemoney',0);
       if($addmoney!=0){
            M('UserAssets')->where('uid='.$id)->setInc('avail_money',$addmoney);
            M('UserAssets')->where('uid='.$id)->setInc('notpresent_money',$addmoney);
            $map=array('status'=>1);
            $paylog = M("AssetsLog");
            $paylog->admin =session('auid');
            $paylog->uid = $id;
            $paylog->amount=$addmoney;
            $paylog->desc='充值';
            $paylog->createdate=date('y-m-d H:m:s',time());
            $paylog->add();
            $this->ajaxReturn($map,'JSON');
            exit();
       }else if($sumemoney!=0){
           $useAss=M('UserAssets')->field('avail_money,present_money,notpresent_money')->where('uid='.$id)->select();
           $useAss=$useAss[0];
           if($useAss['avail_money']<$sumemoney){
               $map=array('status'=>2);
               $this->ajaxReturn($map,'JSON');
               exit();
           }
           if($useAss['notpresent_money']>=$sumemoney){
               M('UserAssets')->where('uid='.$id)->setDec('avail_money',$sumemoney);
               M('UserAssets')->where('uid='.$id)->setDec('notpresent_money',$sumemoney);
           }else{
               M('UserAssets')->where('uid='.$id)->setDec('avail_money',$sumemoney);
               M('UserAssets')->where('uid='.$id)->setField(array('notpresent_money'=>0));
               $cha=$sumemoney-$useAss['notpresent_money'];
               M('UserAssets')->where('uid='.$id)->setDec('present_money',$cha);
           }
           
           $map=array('status'=>1);
           $paylog = M("AssetsLog");
           $paylog->admin =session('auid');
           $paylog->uid = $id;
           $paylog->amount=sumemoney;
           $paylog->desc='扣除';
           $paylog->createdate=date('y-m-d H:m:s',time());
           $paylog->add();
           $this->ajaxReturn($map,'JSON');
           exit();
       }else{
           $map=array('status'=>2);
           $this->ajaxReturn($map,'JSON');
           exit();
       }
  }  

  public function record(){
      $this->display('user/record');
  }
  
public function userpayrecordlist(){

       if(session('auid')==null){
            $this->display('index/login');
            exit();
        }
       $page=I('page',1);
       $rows=I('rows',50);
       $start=($page-1)*$rows;
       $sql='SELECT a.id,a.amount,a.createdate,a.desc,b.id as uid,b.mobile,c.account FROM  dd_assets_log a LEFT JOIN dd_user  b ON a.uid=b.id left join dd_admin c on a.admin=c.id  ORDER BY  a.createdate LIMIT '.$start.','.$rows;
      
       $dao = M();
       $list = $dao->query($sql);   
       $count = M('AssetsLog')->count();
       $map=array(
           'rows'=>$list,
           'total'=>$count
       );
       $this->ajaxReturn($map,'JSON');
       exit();
   }
        

}